const {
  DataTypes
} = require('sequelize');
const DateUtils = require('../utils/dateUtils');
module.exports = sequelize => {
  const attributes = {
    id: {
      type: DataTypes.BIGINT,
      allowNull: false,
      defaultValue: null,
      comment: "主键",
      primaryKey: true,
      field: "id",
      autoIncrement: true
    },
    dictCode: {
      type: DataTypes.CHAR,
      allowNull: true,
      defaultValue: null,
      comment: "关联字典编码，与sys_dict表中的dict_code对应",
      primaryKey: false,
      field: "dict_code",
      autoIncrement: false
    },
    value: {
      type: DataTypes.CHAR,
      allowNull: true,
      defaultValue: null,
      comment: "字典项值",
      primaryKey: false,
      field: "value",
      autoIncrement: false
    },
    label: {
      type: DataTypes.CHAR,
      allowNull: true,
      defaultValue: null,
      comment: "字典项标签",
      primaryKey: false,
      field: "label",
      autoIncrement: false
    },
    tagType: {
      type: DataTypes.CHAR,
      allowNull: true,
      defaultValue: null,
      comment: "标签类型，用于前端样式展示（如success、warning等）",
      primaryKey: false,
      field: "tag_type",
      autoIncrement: false
    },
    status: {
      type: DataTypes.INTEGER,
      allowNull: true,
      defaultValue: null,
      comment: "状态（1-正常，0-禁用）",
      primaryKey: false,
      field: "status",
      autoIncrement: false
    },
    sort: {
      type: DataTypes.INTEGER,
      allowNull: true,
      defaultValue: null,
      comment: "排序",
      primaryKey: false,
      field: "sort",
      autoIncrement: false
    },
    remark: {
      type: DataTypes.CHAR,
      allowNull: true,
      defaultValue: null,
      comment: "备注",
      primaryKey: false,
      field: "remark",
      autoIncrement: false
    },
    createTime: {
      type: DataTypes.DATE,
      allowNull: true,
      defaultValue: null,
      comment: "创建时间",
      primaryKey: false,
      field: "create_time",
      autoIncrement: false,
      get() {
        const rawValue = this.getDataValue('createTime');
        return rawValue ? DateUtils.formatDate(rawValue, 'YYYY-MM-DD HH:mm:ss') : null;
      }
    },
    updateTime: {
      type: DataTypes.DATE,
      allowNull: true,
      defaultValue: null,
      comment: "更新时间",
      primaryKey: false,
      field: "update_time",
      autoIncrement: false,
      get() {
        const rawValue = this.getDataValue('updateTime');
        return rawValue ? DateUtils.formatDate(rawValue, 'YYYY-MM-DD HH:mm:ss') : null;
      }
    }
  };
  const options = {
    tableName: "sys_dict_data",
    comment: "",
    indexes: []
  };
  const SysDictDataModel = sequelize.define("sys_dict_data_model", attributes, options);

  SysDictDataModel.associate = function(models) {
    // 定义与sys_dict的多对一关联
    SysDictDataModel.belongsTo(models.sys_dict_model, {
      foreignKey: 'dictCode',
      targetKey: 'dictCode',
    });
  };
  return SysDictDataModel;
};